package com.nuanshui.heatedloan.msgcenter.apppush;

import com.alibaba.fastjson.JSON;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.AppPushResult;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.Invoker;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.ThirdPartyPushResult;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.ThirdPushMsgInfo;
import com.nuanshui.heatedloan.msgcenter.integration.AppPushMsgBody;
import com.nuanshui.heatedloan.msgcenter.integration.UserDeviceTokenVo;
import com.nuanshui.heatedloan.msgcenter.model.AppPushMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;

/**
 * 执行链的最后一个执行器
 * @param 
 * @author guohao
 * @date 2017年2月10日 下午4:50:11
 * @since $
 */
public class AppPushExecute implements Invoker  {

	private Logger logger = LoggerFactory.getLogger(AppPushExecute.class);
	
	@Override
	public AppPushResult doInvoker(AppPushMessage appPushMessage) {
		AppPushResult appPushResult = new AppPushResult();

		String strMsgBody=JSON.toJSONString(appPushMessage.getMsgBody());

		List<HashMap<String,Object>> userTokens = appPushMessage.getUserTokens();
		for(HashMap<String,Object> userToken : userTokens) {//循环推送

			UserDeviceTokenVo deviceToken=AppPushMessage.convertToken(userToken);

			AppPush appPush =   AppPushFactory.getAppPush(appPushMessage);

			//使用序列化生成新对象
			AppPushMsgBody msgBody=JSON.parseObject(strMsgBody,AppPushMsgBody.class);
			msgBody.setDeviceToken(deviceToken);

		    String result =  appPush.pushMessage(msgBody);
		    appPushResult.addMsg(msgBody);
		    //LogUtil.info(logger, userId+"      "+deviceToken);
		}
	    return appPushResult;
		
	}

}
